﻿// <copyright file="Program.cs" company="Robert Venables">
// Copyright (c) 2009 Robert Venables.
// </copyright>
// <author>Robert Venables</author>
// <email>rob@rvenables.com</email>

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using VNotify;

namespace VNotifyConsoleExample
{
    /// <summary>
    /// Demonstrates using the VNotify library to send several notifications.
    /// </summary>
    internal class Program
    {
        /// <summary>
        /// The (required) API key used for the demonstrations.
        /// </summary>
        private const string ApiKey = "";  // TODO: Update this with a valid API Key.

        /// <summary>
        /// Sends four example notifications with breakpoints in between.
        /// </summary>
        /// <param name="args">Command line arguments (unused)</param>
        public static void Main(string[] args)
        {
            CheckAPIKey();

            Debugger.Break();
            BasicNotificationExample();

            Debugger.Break();
            MinimalNotificationExample();

            Debugger.Break();
            ComplexNotificationExample();

            Debugger.Break();
            AsynchronousNotificationExample();
        }

        #region "Notification Examples"

            /// <summary>
            /// Demonstrates creating recipient and notification objects and then
            /// passing them to the AppNotificationClient to send a message.
            /// </summary>
            private static void BasicNotificationExample()
            {
                // Create new notification client (does the actual sending)
                AppNotificationClient notificationClient = new AppNotificationClient();

                // Define a recipient
                AppNotificationRecipient recipient
                    = new AppNotificationRecipient(ApiKey);

                // Define a notification
                AppNotification helloWorldNotification
                    = new AppNotification("Hello World");

                // Send!
                notificationClient.Send(helloWorldNotification, recipient);

                Console.WriteLine("Sent!");
            }

            /// <summary>
            /// Sends off a "Hello World" notification in 
            /// one line of code (58 characters).
            /// </summary>
            private static void MinimalNotificationExample()
            {
                (new AppNotificationClient()).Send("Hello World", ApiKey);

                Console.WriteLine("Sent!");
            }

            /// <summary>
            /// Demonstrates sending off a fully configured notification object.
            /// </summary>
            private static void ComplexNotificationExample()
            {
                // Create new notification client (does the actual sending)
                AppNotificationClient notificationClient = new AppNotificationClient();

                // Define a recipient (with friendly name)
                AppNotificationRecipient recipient = new AppNotificationRecipient(ApiKey)
                {
                    Name = "James Smith"
                };

                // Define a notification (using all available properties)
                AppNotification helloWorldNotification = new AppNotification()
                {
                    Message = "Hello World (Complex)",
                    Title = "Lorem Ipsum",
                    LongMessage = "<h1>Lorem ipsum dolor sit amet,</h1> consectetur adipisicing elit, <b>sed do eiusmod tempor incididunt</b> ut labore et dolore magna aliqua.",
                    LongMessagePreview = string.Empty,
                    MessageLevel = 1,
                    NotificationButtonText = "Go",
                    RunCommand = "http://www.example.com",
                    Sound = AppNotificationSound.MagicWave,
                    Silent = false
                };

                // Send!
                notificationClient.Send(helloWorldNotification, recipient);

                Console.WriteLine("Sent!");
            }

            /// <summary>
            /// Demonstrates using the APM implementation to send an asynchronous
            /// notification. Alternatively, you could also poll (AsyncToken.IsCompleted)
            /// for completion or pass in a callback (third parameter of BeginSend).
            /// </summary>
            private static void AsynchronousNotificationExample()
            {
                // Create new notification client (does the actual sending)
                AppNotificationClient notificationClient = new AppNotificationClient();

                // Define a recipient
                AppNotificationRecipient recipient
                    = new AppNotificationRecipient(ApiKey);

                // Define a notification
                AppNotification helloWorldNotification
                    = new AppNotification("Hello World");

                Console.WriteLine("Sending Async Message...");

                // Start sending asynchronously
                IAsyncResult asyncToken = notificationClient
                    .BeginSend(helloWorldNotification, recipient, null);

                /*
                 * Notification is now running in other thread and
                 * execution continues here.
                 */ 

                Console.WriteLine("Waiting For Finish...");

                // Block until finished
                notificationClient.EndSend(asyncToken);

                Console.WriteLine("Sent!");
            }

        #endregion

        private static void CheckAPIKey()
        {
            if (ApiKey == string.Empty || ApiKey.Length < 4)
                throw new ApplicationException(
                    "Plug in your API Key Above! (APIKey Constant)");
        }
    }
}